package com.pda.yuanjing.tools;

import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Environment;

import com.pda.yuanjing.PDApp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class StringUtils {
    public static  boolean checkBoxCode(String str){
           if(str.length()>= PDApp.cidInfoBeanOut.getData().getBoxCodeLenMin() &&  str.length()<= PDApp.cidInfoBeanOut.getData().getBoxCodeLenMax()){
               return true;
           }else {
               return false;
           }
       }

    public static boolean checkCaseCode(String str){
        if(str.length()>= PDApp.cidInfoBeanOut.getData().getCaseCodeLenMin() &&  str.length()<= PDApp.cidInfoBeanOut.getData().getCaseCodeLenMax()){
            return true;
        }else {
            return false;
        }
    }

    public static String getJson(Context context, String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = context.getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }


    public static String readTxtToFile(String filePathName) throws IOException {
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + filePathName;
        StringBuffer stringBuffer = new StringBuffer();
        FileInputStream fileInputStream = new FileInputStream(path);
        byte[] buffer = new byte[10240];
        int len = fileInputStream.read(buffer);
        while (len > 0) {
            stringBuffer.append(new String(buffer, 0, len));
            len = fileInputStream.read(buffer);
        }
        fileInputStream.close();
        return stringBuffer.toString();
    }


    public static boolean writeTxtToFile(Context context, String strcontent, String strFilePath) {
        try {
            String path = Environment.getExternalStorageDirectory().getAbsolutePath() + strFilePath;
            File file = new File(path);
            FileOutputStream fos = new FileOutputStream(file,true);
            fos.write(strcontent.getBytes());
            fos.flush();
            fos.close();
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri uri = Uri.fromFile(file);
            intent.setData(uri);
            context.sendBroadcast(intent);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
